package com.fzt.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzt.common.constant.DataScopeEnum;
import com.fzt.common.constant.GlobalCode;
import com.fzt.framework.user.SecurityUser;
import com.fzt.system.convert.SysRoleConvert;
import com.fzt.system.domain.entity.SysRoleEntity;
import com.fzt.system.domain.param.SysRoleParam;
import com.fzt.system.domain.vo.SysRoleVO;
import com.fzt.system.mapper.SysRoleMapper;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 角色服务
 * </p>
 *
 * @author 非洲铜
 * @since 2022-12-11
 */
@Service
@AllArgsConstructor
public class SysRoleService extends ServiceImpl<SysRoleMapper, SysRoleEntity> {
    private final SysRoleMenuService sysRoleMenuService;
    private final SysRoleMapper      sysRoleMapper;

    public SysRoleVO getRole(Long id) {
        SysRoleEntity sysRoleEntity = sysRoleMapper.selectById(id);

        SysRoleVO sysRoleVO = SysRoleConvert.INSTANCE.convert(sysRoleEntity);

        List<Long> menuIdList = sysRoleMenuService.getMenuIdList(id);
        sysRoleVO.setMenuIdList(menuIdList);

        return sysRoleVO;
    }

    public IPage<SysRoleVO> selectPage(SysRoleParam sysRoleParam) {
        Page<SysRoleEntity> pageQuery = new Page<>(sysRoleParam.getPageNum(), sysRoleParam.getPageSize());

        LambdaQueryWrapper<SysRoleEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(sysRoleParam.getName()), SysRoleEntity::getName, sysRoleParam.getName());

        Page<SysRoleEntity> page = sysRoleMapper.selectPage(pageQuery, queryWrapper);

        return page.convert(SysRoleConvert.INSTANCE::convert);
    }


    public List<SysRoleVO> selectList() {
        List<SysRoleEntity> list = list();
        return SysRoleConvert.INSTANCE.convert(list);
    }

    @Transactional(rollbackFor = Exception.class)
    public void save(SysRoleVO sysRoleVO) {
        SysRoleEntity sysRoleEntity = SysRoleConvert.INSTANCE.convert(sysRoleVO);

        Long userId = SecurityUser.getUserId();
        sysRoleEntity.setDataScope(DataScopeEnum.SELF.getValue());
        sysRoleEntity.setDeleted(GlobalCode.Delete.NORMAL);
        sysRoleEntity.setVersion(GlobalCode.Version.BASE_VERSION);
        sysRoleEntity.setCreator(userId);
        sysRoleEntity.setCreateTime(LocalDateTime.now());
        sysRoleEntity.setUpdater(userId);
        sysRoleEntity.setUpdateTime(LocalDateTime.now());

        sysRoleMapper.insert(sysRoleEntity);
        sysRoleMenuService.saveOrUpdate(sysRoleEntity.getId(), sysRoleVO.getMenuIdList());
    }


    @Transactional(rollbackFor = Exception.class)
    public void update(SysRoleVO vo) {
        SysRoleEntity entity = SysRoleConvert.INSTANCE.convert(vo);

        Long userId = SecurityUser.getUserId();
        entity.setUpdater(userId);
        entity.setUpdateTime(LocalDateTime.now());

        // 更新角色
        updateById(entity);

        // 更新角色菜单关系
        sysRoleMenuService.saveOrUpdate(entity.getId(), vo.getMenuIdList());
    }


    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        // 删除角色
        removeByIds(idList);

        // 删除用户角色关系
//        sysUserRoleService.deleteByRoleIdList(idList);

        // 删除角色菜单关系
        sysRoleMenuService.deleteByRoleIdList(idList);

        // 删除角色数据权限关系
//        sysRoleDataScopeService.deleteByRoleIdList(idList);
    }



}
